package cn.lan.generator.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.lan.generator.bean.EntityInfo;
import cn.lan.generator.bean.Module;
import cn.lan.generator.bean.ProjInfo;
import cn.lan.generator.service.CreateFile;
import cn.lan.generator.utils.ConfigUtil;
import cn.lan.generator.utils.Constant;
import cn.lan.generator.utils.FileUtil;
import cn.lan.generator.utils.StringUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lanping
 * @version 1.0
 * @date 2019-07-06
 **/
public class CreateServiceFileImpl implements CreateFile {

    @Override
    public void writeFile(EntityInfo entityInfo, ProjInfo projInfo) throws Exception {

        String tableComment = entityInfo.getTableComment();
        String lowerPrimaryKey = StringUtil.toLowerCaseFirstOne(entityInfo.getPrimaryKey());
        String upperPrimaryKey = StringUtil.toUpperCaseFirstOne(entityInfo.getPrimaryKey());
        String primaryKeyType = entityInfo.getPrimaryKeyType();

        String upperEntityName = StringUtil.toUpperCaseFirstOne(entityInfo.getEntityName());
        String lowerEntityName = StringUtil.toLowerCaseFirstOne(entityInfo.getEntityName());

        String packageName = ConfigUtil.getServicePackage(projInfo);
        String entityPackName = ConfigUtil.getEntityPackage(projInfo);
        String exceptionPackName = ConfigUtil.getExceptionPackage(projInfo.getPackageName());
        String utilsPackage = ConfigUtil.getCommonUtilsPackage(projInfo.getPackageName());
        String dtoPackage = ConfigUtil.getDtoPackage(projInfo);
        String listDtoPackage = ConfigUtil.getListDtoPackage(projInfo);
        String voPackage = ConfigUtil.getVoPackage(projInfo);
        String commonBeanPackage = ConfigUtil.getCommonBeanPackage(projInfo.getPackageName());

        String srcFilePath = FileUtil.getResourceRootDir(projInfo.isDebugModel())+Constant.TEMPLATE_SERVICE_PATH;

        String suffixPath = packageName.replaceAll("\\.", "\\\\") + "\\" + upperEntityName + "Service" + Constant.FILE_SUFFIX_JAVA;
        String destFilePath = Constant.getFilePath(projInfo, Module.getModule(projInfo).getService(),  Constant.PathType.basePath, suffixPath);

        Map<String,String> map = new HashMap<String,String>(){
            {
                put("filePackageName",packageName);
                put("entityPackage",entityPackName);
                put("exceptionPackage",exceptionPackName);
                put("upperEntityName",upperEntityName);
                put("tableComment",tableComment);
                put("projectAuthor",projInfo.getAuthor());
                put("nowDate", DateUtil.today());
                put("lowerEntityName",lowerEntityName);
                put("upperPrimaryKey",upperPrimaryKey);
                put("lowerPrimaryKey",lowerPrimaryKey);
                put("utilsPackage",utilsPackage);
                put("dtoPackage",dtoPackage);
                put("listDtoPackage",listDtoPackage);
                put("voPackage",voPackage);
                put("commonBeanPackage",commonBeanPackage);
                put("primaryType",primaryKeyType);
                put("primaryKeyComment",entityInfo.getPrimaryKeyComment());
            }
        };
        FileUtil.copyFile(projInfo,srcFilePath, destFilePath, map);
    }
}
